define(
	[
		'require',
		'./totalranking_ctrl'
	],
	function(require){
		 function actionFormatter(value, row, index) {
	        return '<div class="form-column-summary" style="text-align:left;width:300px;word-break:break-all;overflow:hidden;" title="'+value+'">'+APM.util.escape(value)+'</div>';
	    }
		var Controller = require('./totalranking_ctrl').extend({
			formTitle: "最慢SQL排行",
			arrayData: Ember.A(['system/response_time/avg']),

			formTopArray: Ember.A([{"name":"Top10","id":"10"},{"name":"Top50","id":"50"},{"name":"Top100","id":"100"}]),

			columns: [
			  [  {
			    "field": "resourceId",
			    "title": "SQL",
			    "colspan": 1,
			    "rowspan": 1,
			    width:300,
			    formatter:actionFormatter
			    // "sortable": true
			  },{
			  	 "field": "system/response_time/avg",
			    "title": "平均响应时间(ms)",
			    "colspan": 1,
			    "rowspan": 1,
			    width:'16%'

			  },{
			  	 "field": "system/response_time/min",
			    "title": "最小(ms)",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "system/response_time/max",
			    "title": "最大(ms)",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "system/throughput/total",
			    "title": "访问次数",
			    "colspan": 1,
			    "rowspan": 1
			  },{
			  	 "field": "system/response_time/total",
			    "title": "运行总时长(ms)",
			    "colspan": 1,
			    "rowspan": 1,
			     width:'16%'
			  }],
			],
			
			formTopValueChange: Ember.observer("formTopValue",function(){
				if(this.get("formTopValue") == 10){
					this.set("chartsWidth",30);
				}else if(this.get("formTopValue") == 50){
					this.set("chartsWidth",20);
				}else if(this.get("formTopValue") == 100){
					this.set("chartsWidth",10);
				}
				this.reportformData();
			}),
			applicationValueChange: Ember.observer("applicationValue",function(){
				this.reportformData();
				
			}),
			/*
				向服务端请求业务系统列表
			*/
			requireApplications: function(){
				var ctrl = this;
				return this.store.ajax("GET", "applications")
				.then(function(response){
					var applications = Ember.A(response.applications);
					ctrl.set("applications",applications);
					ctrl.set("applicationValue",applications[0].id);
					return applications;
				});
			},
			/*
				向服务端请求表格数据
			*/
			reportformData: function(){

				if(!this.get("formTopValue")){
					return;
				}

				if(!this.get("applicationValue")){
					return;
				}

				var ctrl = this;
				ctrl.set("tipTitle","");
				var params = {
					"startDate": +this.get("startDate"),
					"endDate": +this.get("endDate"),
					"appId": ctrl.get("applicationValue"),
					"top": ctrl.get("formTopValue"),
				}
				var newArray = [],
					newObject;

				ctrl.set("tipTitle","");

				return this.store.ajax("GET", "report/sql/slow",params)
				.then(function(response){
					var result = response.result;

					//图表数据数据
					var chartsData = [];
					if(!result[0]){
						ctrl.set("tipTitle","这段时间内暂无数据");
					}

					ctrl.set("chartsData",result);
					ctrl.set("groupData",result);
					setTimeout(function(){
						$(".bootstrap-table .table > tbody > tr").click(function(){
							$(".bootstrap-table .table > tbody > tr").find("td div").addClass("form-column-summary");
							$(this).find("td div").removeClass("form-column-summary");
						});
					},0)
					
				});
			},
			formTopValue:10,
			url:"report/sql/slow/download",
		});

		APM.FormSlowestSQLController = Controller;
		return Controller;
	}
);